package com.zf.common.core.result;

public class RestResponse<T> {

    private String code;

    private String msg;

    private T data;

    private boolean success = true;

    public RestResponse() {
    }

    public RestResponse(String code, String msg, T data, boolean success) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = success;
    }

    public static <T> RestResponse<T> success() {
        return success(null, ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg());
    }

    public static <T> RestResponse<T> success(T data) {
        return success(data, ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg());
    }

    public static <T> RestResponse<T> success(T data, String msg) {
        return success(data, ResultCodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> RestResponse<T> failed() {
        return failed(null, ResultCodeEnum.FAILURE.getCode(), ResultCodeEnum.FAILURE.getMsg());
    }

    public static <T> RestResponse<T> failed(String msg) {
        return failed(null, ResultCodeEnum.FAILURE.getCode(), msg);
    }

    public static <T> RestResponse<T> failed(ResultCodeEnum resultCodeEnum) {
        return failed(null, resultCodeEnum.getCode(), resultCodeEnum.getMsg());
    }

    public static <T> RestResponse<T> failed(ResultCodeEnum resultCodeEnum, String message) {
        return failed(null, resultCodeEnum.getCode(), message);
    }

    public static <T> RestResponse<T> failedWithData(T data) {
        return failed(data, ResultCodeEnum.FAILURE.getCode(), ResultCodeEnum.FAILURE.getMsg());
    }

    public static <T> RestResponse<T> failedWithData(T data, String msg) {
        return failed(data, ResultCodeEnum.FAILURE.getCode(), msg);
    }

    public static <T> RestResponse<T> failed(String code, String msg) {
        return failed(null, code, msg);
    }

    public static <T> RestResponse<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return failed();
        }
    }

    public static <T> RestResponse<T> success(T data, String code, String msg) {
        return build(data, code, msg, true);
    }


    public static <T> RestResponse<T> failed(T data, String code, String msg) {
        return build(data, code, msg, false);
    }

    private static <T> RestResponse<T> build(T data, String code, String msg, boolean success) {
        RestResponse<T> restResponse = new RestResponse<>();
        restResponse.setCode(code);
        restResponse.setData(data);
        restResponse.setMsg(msg);
        restResponse.setSuccess(success);
        return restResponse;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}
